package common;

import sun.applet.Main;

import java.awt.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 应用模块名称<p> 客户端类型
 * 代码描述<p>
 * copyright:
 * Company: 上海悟泰信息科技有限公司
 *
 * @author
 * @since 2019/10/30 09:46
 */
public  class  StringTest {
    public static void main(String[] args) throws ParseException {

        String str = "hello";
        String str1 = "hello";
        System.out.println(str == str1);

        Integer i = 127;
        Integer i2 = 127;
        System.out.println(i == i2);

//        String result = "12.00";
//        BigDecimal decimal = new BigDecimal(result);
//
//        System.out.println(decimal);

//        String res = "^\\d{0,1}$";
//       String re = "^\\d{0,3}+(.\\d{0,2})?$";
//        String rs = "[1-9]{1}\\d{6,7}|0\\d{2,2}[-]?[1-9]{1}\\d{6,7}|" +
//                "0\\d{2,2}\\s?[1-9]{1}\\d{6,7}|0\\d{2,3}[-]?[1-9]{1}\\d{6,7}|" +
//                "0\\d{2,3}\\s?[1-9]{1}\\d{6,7}|[1][3,4,5,7,8][0-9]{9}";
//        Pattern pattern = Pattern.compile(res);
//        Matcher matcher = pattern.matcher("0");
//        System.out.println(matcher.matches());

//        boolean satisfyYear = isSatisfyYear("420581201912150014");
//        System.out.println(satisfyYear);

//        String random = getRandom(9);
//        System.out.println(random);

//        String lastFour = getIdcardLastFour("420581201912150014");
//        System.out.println(lastFour);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date1 = sdf.parse("2019-11-17 1:35:21");
//        Date date2 = new Date();
//        Long day = getDifferenceTime(date1, date2);
//        System.out.println(day);
//        Double height = 178D;
//        Double aDouble = Double.valueOf(String.format("%.2f", height / 100));
//        System.out.println(aDouble);

//        System.out.println(idMask("420581199609190014", 10, 4));

//        String aa = "1、异常1\n2、异常2";
//
//        Pattern p = Pattern.compile("\r\n|\n");
//        Matcher m = p.matcher(aa);
//        String replaceAll = m.replaceAll(";");
//        System.out.println(replaceAll);

//        BigDecimal height = new BigDecimal(150);
//        BigDecimal weight = new BigDecimal(100);
//        BigDecimal bigDecimal = calculateBmi(weight, height);
//        System.out.println(bigDecimal);

    }

    /**
     * 校验字符串小数位2位
     *
     * @param numeral 需要校验的数字
     */
    private static boolean isTwoDecimal(String numeral) {
        if (numeral == null || numeral.length() == 0) {
            return false;
        }
        try {
            int length = numeral.length();
            System.out.println(length);
            int index = numeral.indexOf(".");
            System.out.println(index);
            return length - index -1 == 2;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 计算两个时间相差小时数
     *
     * @param date1 时间1
     * @param date2 时间2
     * @return 两个时间相差小时数
     */
    public static Long getDifferenceTime(Date date1, Date date2) {
        long l = date2.getTime() - date1.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        return (l / (60 * 60 * 1000));
    }


    /**
     * 非法的年龄，用于处理异常
     */
    private static final int INVALID_AGE = -1;

    /**
     * 根据身份证号码计算年龄
     *
     * @param idCardNo 考虑到了15位身份证，但不一定存在
     */
    public static int getAgeByIdCardNo(String idCardNo) {
        String dateStr;
        if (idCardNo.length() == 15) {
            dateStr = "19" + idCardNo.substring(6, 12);
        } else if (idCardNo.length() == 18) {
            dateStr = idCardNo.substring(6, 14);
        } else {
            //默认是合法身份证号，但不排除有意外发生
            return INVALID_AGE;
        }


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date birthday = simpleDateFormat.parse(dateStr);
            return getAgeByDate(birthday);
        } catch (ParseException e) {
            return INVALID_AGE;
        }
    }


    private static int getAgeByDate(Date birthday) {
        Calendar calendar = Calendar.getInstance();
        if (calendar.getTimeInMillis() - birthday.getTime() < 0L) {
            return INVALID_AGE;
        }
        int yearNow = calendar.get(Calendar.YEAR);
        int monthNow = calendar.get(Calendar.MONTH);
        int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.setTime(birthday);
        int yearBirthday = calendar.get(Calendar.YEAR);
        int monthBirthday = calendar.get(Calendar.MONTH);
        int dayOfMonthBirthday = calendar.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirthday;
        if (monthNow == monthBirthday && dayOfMonthNow < dayOfMonthBirthday || monthNow < monthBirthday) {
            age--;
        }
        return age;
    }

    public static boolean isSatisfyYear(String idCardNo) {
        String year = idCardNo.substring(6, 10);
        String month = idCardNo.substring(10, 12);
        String day = idCardNo.substring(12, 14);
        int yearNum = Integer.parseInt(year);
        int monthNum = Integer.parseInt(month);
        int dayNum = Integer.parseInt(day);
        Calendar calendar = Calendar.getInstance();
        int curYeay = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH) + 1;
        int curDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (yearNum > curYeay) {
            return false;
        } else if (yearNum == curYeay) {
            if (monthNum > curMonth) {
                return false;
            }
            if (monthNum == curMonth) {
                return dayNum <= curDay;
            }
        }
        return true;
    }

    /**
     * 生成指定位数的随机数
     *
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val.append(random.nextInt(10));
        }
        return val.toString();
    }

    public static String getIdcardLastFour(String idcardNo) {
        if (idcardNo == null || "".equals(idcardNo)) {
            return null;
        }
        if (idcardNo.length() < 4) {
            return null;
        }
        return idcardNo.substring(idcardNo.length() - 4);
    }

    public static Long getDifferenceDay(Date date1, Date date2) {
        return (date2.getTime() - date1.getTime()) / (60 * 60 * 24 * 1000);
    }

    /**
     * 用户身份证号码的打码隐藏加星号加*
     *
     * @param idcardNo 身份证号码
     * @param front    需要显示前几位
     * @param end      需要显示末几位
     * @return 处理完成的身份证
     */
    public static String idMask(String idcardNo, int front, int end) {
        //身份证不能为空
        if (idcardNo == null || "".equals(idcardNo)) {
            return null;
        }
        //需要截取的长度不能大于身份证号长度
        if ((front + end) > idcardNo.length()) {
            return null;
        }
        //需要截取的不能小于0
        if (front < 0 || end < 0) {
            return null;
        }
        //计算*的数量
        int asteriskCount = idcardNo.length() - (front + end);
        StringBuilder asteriskStr = new StringBuilder();
        for (int i = 0; i < asteriskCount; i++) {
            asteriskStr.append("*");
        }
        String regex = "(\\w{" + front + "})(\\w+)(\\w{" + end + "})";
        return idcardNo.replaceAll(regex, "$1" + asteriskStr + "$3");
    }

    /**
     * 计算体质指数
     *
     * @param weight 身高[cm]
     * @param height 体重[KG]
     * @return 体质指数
     */
    public static BigDecimal calculateBmi(BigDecimal weight, BigDecimal height) {
        if (weight == null) {
            return null;
        } else if (height == null) {
            return null;
        } else if (weight.intValue() == 0) {
            return new BigDecimal("0");
        } else if (height.intValue() == 0) {
            return new BigDecimal("0");
        } else {
            BigDecimal heightM = height.divide(new BigDecimal(100));
            BigDecimal bmi = weight.divide(heightM.pow(2), 2, RoundingMode.HALF_UP);
            BigDecimal maxValue = new BigDecimal("999");
            return bmi.compareTo(maxValue) >= 1 ? maxValue : bmi;
        }
    }



}

